﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BackEnd.Common;
using BackEnd.Models;
using BusinessLayer.WorldArtPrice;
using BusinessLayer.WorldArtPrice.Entities;
using BackEnd.Models;

namespace BackEnd.Controllers
{
    public class ItemTypeController : Controller
    {
        //
        // GET: /ItemType/

        [CustomAuthorize(Action = Constants.AdminActions.ListItemType)]
        public ActionResult Index()
        {
            return View();
        }

        [CustomAuthorize(Action = Constants.AdminActions.CreateItemType)]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.CreateItemType)]
        public ActionResult Create(ItemTypeModel itemTypeModel)
        {
            if (string.IsNullOrWhiteSpace(itemTypeModel.Name))
            {
                ModelState.AddModelError("Name", "Name is not null");
            }
            
            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new ItemTypeBL().Insert(new ItemType { Name = itemTypeModel.Name});
                return RedirectToAction("Index", "ItemType");
            }
        }

        [CustomAuthorize(Action = Constants.AdminActions.EditItemType)]
        public ActionResult Edit(int id)
        {
            ItemTypeModel itemTypeModel = new ItemTypeModel();
            ItemType itemType = new ItemTypeBL().GetById(id);
            itemTypeModel.Name = itemType.Name;
            return View(itemTypeModel);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.EditItemType)]
        public ActionResult Edit(ItemTypeModel itemTypeModel)
        {
            if (string.IsNullOrWhiteSpace(itemTypeModel.Name))
            {
                ModelState.AddModelError("Name", "Name is not null");
            }

            if (ModelState.IsValid == false)
            {
                return View();
            }
            else
            {
                new ItemTypeBL().Update(new ItemType {Id = itemTypeModel.Id, Name = itemTypeModel.Name });
                return RedirectToAction("Index", "ItemType");
            }
        }

        public JsonResult Search(string name, int pageIndex, int pageSize)
        {
            int totalRecord = 0;
            List<ItemType> listItemType = new ItemTypeBL().Search(name, pageIndex, pageSize, out totalRecord);

            ListObjectModel<ItemType> listObject = new ListObjectModel<ItemType> { ListItem = listItemType, PageIndex = pageIndex, PageSize = pageSize, TotalRecord = totalRecord };
            return Json(listObject, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        [CustomAuthorize(Action = Constants.AdminActions.DeleteItemType)]
        public JsonResult Delete(string listId)
        {            

            string[] arrayId = listId.Split('_');
            for (int i = 0; i < arrayId.Length; i++)
            {
                new ItemTypeBL().Delete(Convert.ToInt32(arrayId[i]));                
            }
            return Json(1, JsonRequestBehavior.AllowGet);
        }

    }
}
